<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('basic');
	$return['list']=array();
	$stores=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id <>'=>$this->store_id));
	$return['stores']=$stores;
	$return['allstores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']));
	set_html('title',$$basic['title']?$basic['title']:'商品列表');
	$return['params']=array();
	$return['is_admin']=$this->is_admin;
	$return['post']=array('morenum'=>array());
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>0,
		'status'=>1,
	);
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if($_RQ['pcate']){
		$params['pcate']=intval($_RQ['pcate']);
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	if($_RQ['ccate']){
		$params['ccate']=intval($_RQ['ccate']);
		$return['params']['ccate']=intval($_RQ['ccate']);
	}
	if($_RQ['order']=='isrecommand'){
		$params['saletype likes']=array('1');
		$return['params']['order']='isrecommand';
	}
	if($_RQ['order']=='isnew'){
		$params['saletype likes']=array('2');
		$return['params']['order']='isnew';
	}
	if($_RQ['order']=='ishot'){
		$params['saletype likes']=array('3');
		$return['params']['order']='ishot';
	}
	if($_RQ['priceMin']){
		$params['price >=']=intval($_RQ['priceMin']);
		$return['params']['priceMin']=intval($_RQ['priceMin']);
	}
	if($_RQ['priceMax']){
		$params['price <=']=intval($_RQ['priceMax']);
		$return['params']['priceMax']=intval($_RQ['priceMax']);
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','title','subtitle','image','video','price','active_plugin','active_id','salenum','total','pluginname','originalprice');
	$orderby='star desc, paixu desc, salenum desc , id desc ';
	if($_RQ['order']){
		if($_RQ['order']=='sales'){
			$orderby='salerealnum desc';
		}
		if($_RQ['order']=='price'){
			if($_RQ['order_type']==2){
				$orderby='price desc';
			}else{
				$orderby='price asc';
			}
			
		}
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	$list=model('jy_weishop/goods')->goods_list($parameter,$total);
	if(!$list){
		exi('none');
	}else{
		if($this->plugin_exists('jy_merch')){
			$jy_merch=true;
		}
		$storeids=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),'id','id');
		if($storeids){
			$storeids=array_keys($storeids);
		}else{
			$storeids=-1;
		}
		foreach($list as &$itemv){
			$itemv['oldtotal']=$itemv['total'];
			$store_goods=pdo_get('jy_weishop_store_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],'storeid'=>$this->store_id),array('id','total'));
			if($store_goods['id']){
				$itemv['total']=$store_goods['total'];
			}else{
				$data=array(
							'uniacid'=>$_SESSION['uniacid'],
							'goodsid'=>intval($itemv['id']),
							'storeid'=>intval($this->store_id),
							'total'=>intval($itemv['total']),
							'price'=>$itemv['price'],
				);
				pdo_insert('jy_weishop_store_goods',$data);
			}
			$itemv['alltotal']=pdo_getsum('jy_weishop_store_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id'],'storeid'=>$storeids),'total');
			$itemv['originalprice']=$itemv['originalprice']>0?$itemv['originalprice']:price($itemv['price']*1.2);
			$itemv['createtime']=$itemv['createtime']?:time()-58*3600*24;
			$itemv['createtime']=date('Y-m-d',$itemv['createtime']);
			$itemv['likes']=pdo_count('jy_weishop_member_history',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$itemv['id']),'id');
		}
	}
	exi($list);
}
if($_RQ['op']=='settotal'){
	$postdata=get_postdata();
	$goodsid=intval($postdata['goodsid']);
	$goodsids=str_array($postdata['goodsids']);
	$ispi=intval($postdata['ispi']);
	if(!$goodsid && !$ispi){
		exi('请选择商品','error');
	}
	if(!count($goodsids) && $ispi){
		exi('请选择商品!','error');
	}
	if(!$ispi){
		$goodsids[]=$goodsid;
	}
	if(!$postdata['num'] && $postdata['type']!='inmore'){
		exi('请填入数量！','error');
	}
	if($postdata['type']=='diao'){
		if(!$postdata['tostoreid']){
			exi('请选择调入门店！','error');
		}
	}
	foreach($goodsids as $v){
		if($postdata['type']=='diao'){
			$store_goods=pdo_get('jy_weishop_store_goods',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$v,'storeid'=>$this->store_id),array('id','total'));
			$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v),array('id','total','title'));
			if(!$store_goods['id']){
				$data=array(
							'uniacid'=>$_SESSION['uniacid'],
							'goodsid'=>$v,
							'storeid'=>$postdata['tostoreid'],
							'total'=>intval($goods['total']),
							'price'=>$goods['price'],
				);
				pdo_insert('jy_weishop_store_goods',$data);
			}
		}
		if($postdata['type']=='out' || $postdata['type']=='diao'){
			$lsastnum=pdo_getcolumn('jy_weishop_store_goods',array('goodsid'=>$v,'storeid'=>$this->store_id),'total');
			if($lsastnum<$postdata['num']){
				exi($goods['title'].'库存数量不足！当前仅剩库存'.$lsastnum,'error');
			}
		}
		if($postdata['type']=='diao'){
			pdo_update('jy_weishop_store_goods',array('total +='=>$postdata['num']),array('goodsid'=>$v,'storeid'=>$postdata['tostoreid'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($postdata['type']=='out' || $postdata['type']=='diao'){
			pdo_update('jy_weishop_store_goods',array('total -='=>$postdata['num']),array('goodsid'=>$v,'storeid'=>$this->store_id,'uniacid'=>$_SESSION['uniacid']));
		}elseif($postdata['type']=='in'){
			pdo_update('jy_weishop_store_goods',array('total +='=>$postdata['num']),array('goodsid'=>$v,'storeid'=>$this->store_id,'uniacid'=>$_SESSION['uniacid']));
		}
		if($postdata['type']=='out'){
			$this->model('model')->checkquan('jy_shopwms/manage.goods/out/');
			$outtype=1;
		}elseif($postdata['type']=='in'){
			$this->model('model')->checkquan('jy_shopwms/manage.goods/out/');
			$intype=1;
		}elseif($postdata['type']=='diao'){
			$outtype=2;
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'goods_id'=>$v,
				'store_id'=>$postdata['tostoreid'],
				'num'=>intval($postdata['num']),
				'type'=>1,
				'intype'=>2,
				'outtype'=>0,
				'remark'=>$postdata['remark'],
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_shopwms_log',$data);
		}elseif($postdata['type']=='inmore'){
			foreach($postdata['morenum'] as $id=>$pv){
				if($pv>0){
					$data=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$_SESSION['uid'],
						'goods_id'=>$v,
						'store_id'=>$id,
						'num'=>intval($pv),
						'type'=>1,
						'intype'=>1,
						'outtype'=>0,
						'remark'=>$postdata['remark'],
						'createtime'=>TIMESTAMP,
					);
					pdo_insert('jy_shopwms_log',$data);
					pdo_update('jy_weishop_store_goods',array('total +='=>$pv),array('goodsid'=>$v,'storeid'=>$id,'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
		if($postdata['type']!='inmore'){
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'goods_id'=>$v,
				'store_id'=>$this->store_id,
				'num'=>intval($postdata['num']),
				'type'=>$postdata['type']!='in'?0:1,
				'intype'=>$intype,
				'outtype'=>$outtype,
				'remark'=>$postdata['remark'],
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_shopwms_log',$data);
		}
	}
	exi('操作成功！','','//main/');
}
?>